home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 4403 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.2 KB  |  99 lines

  1. Path: gail.ripco.com!mambuhl
  2. From: mambuhl@ripco.com (Martin Ambuhl)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need help on program
  5. Date: 4 Feb 1996 05:37:37 GMT
  6. Organization: Ripco Communications, Inc.
  7. Message-ID: <4f1gn1$kup@gail.ripco.com>
  8. NNTP-Posting-Host: golden.ripco.com
  9.  
  10. phaley@saucer.cc.umr.edu (Paul Haley) in <4f0vat$j8j@hptemp1.cc.umr.edu>
  11. asks:
  12.  
  13. >        Hello.  I'm trying to write a program which will count the total
  14. >amount of a "poker" jackpot.  I read the input into an array, then read
  15. >the array values in, and take appropriate action.
  16. >        c = call
  17. >        d = drop
  18. >        5 players are in the game
  19.  
  20. >        Example input would be:
  21. >        5cccc
  22. >        5cc10cc
  23. >        100cc5cc
  24.  
  25. >        My problem with the program, is that the only time the program is
  26. >100% correct is when I have a single digit number followed by c's or d's.
  27. >Anything else will give a slightly higher answer than I need.  Here is
  28. >the function.
  29.  
  30. The problem with the code is in your use of atoi().  In the slightly
  31. changed code below, my changes and comments are marked with /* mha ... */.
  32. Some of my changes (i.e. commenting out unused vars) are to make it
  33. easier for me to read the code and are not crucial.
  34.  
  35. #include <ctype.h>              /* mha - added */
  36. #include <stdio.h>              /* mha - added */
  37. void end_game(int);             /* mha - added */
  38.  
  39. void array_check(int i, char storage[])
  40. {
  41.     int pot = 0, j = 0, temp = 0, old_call_value = 0;
  42.     int call_value = 0, prev = 0;
  43. #if 0
  44.     /* mha - unused variables commented out */
  45.     char temp2;
  46.     int raise = 0, num_plays = 0, c = 0, count = 0;
  47. #endif
  48.  
  49.     for (j = 0; j < i; ++j) {
  50.         if (isdigit(storage[j]) != 0) {
  51.             if (prev == 1) {    /* prev means there was a previous
  52.                                  * digit */
  53. #if 0
  54.     /* mha - replaced code commented out */
  55.                 temp2 = storage[j];
  56.                 c = atoi(&temp2);
  57.                 temp = temp * 10 + c;
  58. #endif
  59.                 /* mha - the above use of atoi is wrong. atoi() is for
  60.                  * converting strings to ints, not for converting
  61.                  * digits to their equivalents.  */
  62.                 temp = temp * 10 + storage[j] - '0';    /* mha - new */
  63.                 prev = 1;
  64.             } else {
  65. #if 0
  66.     /* mha - replaced code commented out */
  67.                 temp2 = storage[j];
  68.                 temp = atoi(&temp2);
  69. #endif
  70.                 temp = storage[j] - '0';    /* mha - see above comments */
  71.                 prev = 1;
  72.             }
  73.             old_call_value = call_value;
  74.             puts("call_value=temp");
  75.             call_value += temp;
  76.  
  77.         }
  78.         if (storage[j] == 'c') {/* begin 'c' check */
  79.             printf("storage[%d] = 'c'\n", j);
  80.             old_call_value = 0;
  81.             prev = 0;
  82.         }                       /* end 'c' check */
  83.         if (storage[j] == 'd') {/* begin 'd' check */
  84.             old_call_value = 0;
  85.             prev = 0;
  86.             puts("storage[j] = d and prev is set to 0");
  87.         }
  88.         /* end 'd' check */
  89.         else
  90.             pot += (call_value - old_call_value);
  91.     }
  92.     end_game(pot);
  93. }
  94.  
  95.                                                                              
  96. --
  97. * Martin Ambuhl       net: mambuhl@ripco.com
  98. * Chicago, IL (USA)    
  99.